/**
* This file is part of OntoCompoFramework
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.ui.positioning;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;

import javax.swing.JComponent;

import ontocompo.ui.screenmanagement.Screen;

public class PositioningDropTarget implements DropTargetListener {

	private Color colorBackup;
	
	@Override
	public void dragEnter(DropTargetDragEvent dtde) {
		// TODO Auto-generated method stub
//		System.err.println("dragEnter");
		duringDrag(dtde);
	}

	@Override
	public void dragExit(DropTargetEvent dtde) {
		// TODO Auto-generated method stub
//		System.err.println("dragExit");
		if(this.colorBackup != null) {
			dtde.getDropTargetContext().getComponent().setBackground(this.colorBackup);
			this.colorBackup = null;
		}
	}

	@Override
	public void dragOver(DropTargetDragEvent dtde) {
		// TODO Auto-generated method stub
		duringDrag(dtde);
	}

	@Override
	public void drop(DropTargetDropEvent dtde) {
		// TODO Auto-generated method stub
//		System.err.println("drop");
		dtde.acceptDrop(dtde.getDropAction());
		Transferable tr = dtde.getTransferable();
		
		if(tr.isDataFlavorSupported(TransferableJComponent.jComponentDataFlavor)) {
			try {
				JComponent jComponentValue = (JComponent) tr.getTransferData(TransferableJComponent.jComponentDataFlavor);
//				System.err.println(jComponentValue);
//				System.err.println(dtde.getSource());
				
				if(dtde.getDropTargetContext().getComponent().getClass().isAssignableFrom(PositioningPane.class)) {
					if(PositioningPane.selectedScreen.getRootScreen() == null) {
						PositioningPane pp = (PositioningPane) dtde.getDropTargetContext().getComponent();
//						System.err.println(pp);
						pp.transfertJComponent(jComponentValue);
					
						dtde.dropComplete(true);
					} else {
						dtde.dropComplete(false);
					}
					
					if(this.colorBackup != null) {
						dtde.getDropTargetContext().getComponent().setBackground(this.colorBackup);
						this.colorBackup = null;
					}
					
				} else if(dtde.getDropTargetContext().getComponent().getClass().isAssignableFrom(RelativePosition.class)) {
					RelativePosition rp = (RelativePosition) dtde.getDropTargetContext().getComponent();
					GridRelativePane grp = (GridRelativePane) rp.getParent();
					
					jComponentValue.getParent().remove(jComponentValue);
					PositioningPane.selectedScreen.removeFromBulkList(jComponentValue);
					
					switch(rp.getPosition()) {
						case TOPLEFT:
							grp.addOnTopLeft(jComponentValue);
							break;
						case TOP:
							grp.addOnTop(jComponentValue);
							break;
						case TOPRIGHT:
							grp.addOnTopRight(jComponentValue);
							break;
						case LEFT:
							grp.addOnLeft(jComponentValue);
							break;
						case RIGHT:
							grp.addOnRight(jComponentValue);
							break;
						case BOTTOMLEFT:
							grp.addOnBottomLeft(jComponentValue);
							break;
						case BOTTOM:
							grp.addOnBottom(jComponentValue);
							break;
						case BOTTOMRIGHT:
							grp.addOnBottomRight(jComponentValue);
							break;
						default: ;
							
					}
					
					if(this.colorBackup != null) {
						dtde.getDropTargetContext().getComponent().setBackground(this.colorBackup);
						this.colorBackup = null;
					}
					
					dtde.dropComplete(true);
				}
				//Screen selectedScreen = pp.getSelectedScreen();
				//System.err.println(selectedScreen);
				
				//selectedScreen.add(jComponentValue);
				
				dtde.dropComplete(false);
			} catch(UnsupportedFlavorException e) {
				dtde.dropComplete(false);
				e.printStackTrace();
			} catch(IOException e) {
				dtde.dropComplete(false);
				e.printStackTrace();
			}
		}
	}

	@Override
	public void dropActionChanged(DropTargetDragEvent dtde) {
		// TODO Auto-generated method stub
//		System.err.println("dropActionChanged");
	}
	
	private void duringDrag(DropTargetDragEvent dtde) {
		if(! dtde.isDataFlavorSupported(TransferableJComponent.jComponentDataFlavor)) {
			dtde.rejectDrag();
		} else {
//			setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) );
			if(this.colorBackup == null) {
				this.colorBackup = dtde.getDropTargetContext().getComponent().getBackground();
				dtde.getDropTargetContext().getComponent().setBackground(Color.GREEN);
			}
		}
	}

}
